// pages/invoice/invoice.js
var that,timer,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  qrcode = require('../../utils/qrcode.js'),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // ischeck:0,
    orderRecordlist:[],
    isall:!1,
    way:'scene',
    cateId:2,
    applyFee:0,
    idarr:[],
    stationId:0,
    isload:!1,
    stationname:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this
    let page=getCurrentPages(),stationname=''
    page = page[page.length-2];
    console.log(page,'pagepagepage');
    let {refulinglist}=page.data
    console.log(refulinglist,'refulinglistrefulinglist');
    refulinglist.forEach(x=>{
      if(x.id==options.id){
        stationname=x.name
      }
    })
    console.log(options,'optionsoptionsoptions');
    that.setData({stationId:options.id,isload:!0,stationname},()=>{
      that.getdata()
    })
  },
  getdata(){
    let {cateId,stationId}=that.data,orderRecordlist=[]
    api.invoiceinfo({cateId,stationId},res=>{
      if(res.data.code==0){
        let {canFee,orderArr}=res.data.entity.data
        orderArr.forEach(x=>{
          x.invoiceFeearr=Number(x.invoiceFee).toFixed(2).split('.')
          x.addTime=util.formatTime1(new Date(x.addTime*1000))
          x.endTime=util.formatTime1(new Date(x.endTime*1000))
          x.state=!1
          orderRecordlist.push(x)
        })
        that.setData({orderRecordlist,canFee,applyFee:0,})
      }
    })
  },
  inputtap(e){
    let { value } = e.detail,{orderRecordlist}=that.data
    orderRecordlist.forEach(x=>{
      x.state=!1
    })
    that.setData({applyFee:value,orderRecordlist})
  },
  tosel(e){
    let {index} = e.currentTarget.dataset,{cateId,orderRecordlist} = that.data
    if(index==cateId){
      return
    }
    that.setData({cateId:index,orderRecordlist:[],applyFee:0},()=>{
      that.getdata()
    })
  },
  selectall(){
    let {isall,orderRecordlist} = that.data,applyFee=0
    if(!isall){
      orderRecordlist.forEach((x,i)=>{
        // console.log(x,'ggghgh');
        x.state=!0
        applyFee+=parseFloat(x.invoiceFee)
      })
    }else{
      orderRecordlist.forEach((x,i)=>{
        // console.log(x,'ggghgh');
        x.state=!1,
        applyFee=0
      })
    }
      that.setData({isall:!isall,orderRecordlist,applyFee},()=>{})
  },
  tochoose(e){
    let {id} = e.currentTarget.dataset,{orderRecordlist,applyFee} = that.data
    orderRecordlist.forEach((x,i)=>{
      if(x.id==id){
        if(x.state){
          applyFee=Number(applyFee)-parseFloat(x.invoiceFee)
        }else{
          applyFee=Number(applyFee)+parseFloat(x.invoiceFee)
        }
        // console.log(x,'ggghgh');
        x.state=!x.state
      }
    })
    that.setData({orderRecordlist,applyFee},()=>{})
  },
  way(e){
  let {info} = e.currentTarget.dataset,{way,orderRecordlist}=that.data
  if(info==way){
    return
  }
  orderRecordlist.forEach(x=>{
    x.state=!1
  })
  that.setData({way:info,orderRecordlist,isall:!1,applyFee:0})
  },
  // 开发票
  drawbill(){
    let {cateId,way,stationId,applyFee,orderRecordlist,canFee}=that.data,err='',idarr=[]
      orderRecordlist.forEach(x=>{
        if(x.state){
          idarr.push(x.id)
        }
      })
      if(cateId==1){
        if(idarr.length==0)err="请选择订单"
      }
      if(applyFee==''||applyFee<=0)err="开具的金额要大于0"
      if(canFee<applyFee)err="开具的金额要小于可开金额"
      if(err!=''){
        wx.showModal({
          content:err,
          showCancel:!1,
        })
        return;
      }
    let ids=idarr.toString()
    if(way=='scene'){
       let data={applyFee:Number(applyFee),ids,reTypeId:1,cateId,stationId,link_phone:''}
      
      api.invoiceapply(data,res=>{
        if(res.data.code==0){
          that.setData({qrStr:res.data.entity.data.qrStr,showmodal:!0},()=>{
            that.getSystemInfo()
          })
          // wx.navigateTo({
          //   url: '../InvoiceSucc/InvoiceSucc',
          // })
        }else{
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      })
    }else{
      wx.navigateTo({
        url: '../WriteAnInvoice/WriteAnInvoice?stationId='+stationId,
      })
    }
  },
  getSystemInfo(){
    wx.showLoading({
      title: '加载中',
    })
    wx.getSystemInfo({
      success: (res) => {
        new qrcode({
          width: res.screenWidth * 0.515,
          height: res.screenWidth * 0.515,
          canvasId: 'canvas',
          text: that.data.qrStr,
          callback: res => {
            wx.hideLoading();
            that.getisDeal()
          }
        })
      },
    })
  },
  getisDeal(){
    let {qrStr,stationId}=that.data
    timer=setInterval(function () {
      api.invoicedealstatus(qrStr,res=>{
        if(res.data.code==0){
          if(res.data.entity.data.isDeal==1){
            console.log(1111);
            clearInterval(timer);
            that.setData({showmodal:!1},()=>{
              wx.navigateTo({
                url: '../InvoiceSucc/InvoiceSucc?stationId='+stationId,
              })
            })
          }
        }else{
          that.setData({showmodal:!1})
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      })
    },3000)
    that.setData({timer})
  },
  closemodal(){
    that.setData({showmodal:!1},()=>{
      clearInterval(that.data.timer);
      that.getdata()
    })
  },
  to(e){
    let {url}=e.currentTarget.dataset,{stationId,cateId}=that.data
    wx.navigateTo({
      url: `../${url}/${url}?stationId=${stationId}&cateId=${cateId}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let {isload}=that.data
    if(isload){
      console.log(1111);
      
      that.setData({orderRecordlist:[]},()=>{
        that.getdata()
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})